/*
 * ******************************************************************************
 *   Copyright (c) 2013-2014 Gabriele Mariotti.
 *
 *   Licensed under the Apache License, Version 2.0 (the "License");
 *   you may not use this file except in compliance with the License.
 *   You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 *   Unless required by applicable law or agreed to in writing, software
 *   distributed under the License is distributed on an "AS IS" BASIS,
 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *   See the License for the specific language governing permissions and
 *   limitations under the License.
 *  *****************************************************************************
 */

package it.gmariotti.cardslib.demo.drawable;

import ohos.agp.components.element.Element;
import ohos.agp.components.element.ShapeElement;
import ohos.agp.render.*;
import ohos.agp.utils.Color;
import ohos.agp.utils.Rect;
import ohos.media.image.PixelMap;

import static ohos.agp.render.Paint.ShaderType.PIXELMAP_SHADER;


/**
 * Image with rounded corners
 * <p/>
 * You can find the original source here:
 *
 *
 * @author Gabriele Mariotti (gabri.mariotti@gmail.com)
 */
public class CircleDrawable extends ShapeElement {

    private final PixelMapShader mBitmapShader;
    private final Paint mPaint;
    private Paint mWhitePaint;
    int circleCenterX;
    int circleCenterY;
    int mRadus;
    private boolean mUseStroke = false;
    private int mStrokePadding = 0;

    public CircleDrawable(PixelMap bitmap) {

        mBitmapShader = new PixelMapShader(new PixelMapHolder(bitmap),
                Shader.TileMode.CLAMP_TILEMODE, Shader.TileMode.CLAMP_TILEMODE);

        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setShader(mBitmapShader, PIXELMAP_SHADER);

    }

    public CircleDrawable(PixelMap bitmap, boolean mUseStroke) {
        this(bitmap);

        if (mUseStroke) {
            this.mUseStroke = true;
            mStrokePadding = 4;
            mWhitePaint = new Paint();
            mWhitePaint.setStyle(Paint.Style.FILLANDSTROKE_STYLE);
            mWhitePaint.setStrokeWidth(0.75f);
            mWhitePaint.setColor(Color.WHITE);
        }
    }

    @Override
    public void setBounds(Rect bounds) {
        super.setBounds(bounds);
        circleCenterX = bounds.getWidth() / 2;
        circleCenterY = bounds.getHeight() / 2;

        if (bounds.getWidth() >= bounds.getHeight())
            mRadus = bounds.getWidth() / 2;
        else
            mRadus = bounds.getHeight() / 2;
    }


    @Override
    public void drawToCanvas(Canvas canvas) {
        if (mUseStroke) {
            canvas.drawCircle(circleCenterX, circleCenterY, mRadus, mWhitePaint);
        }
        canvas.drawCircle(circleCenterX, circleCenterY, mRadus - mStrokePadding, mPaint);
    }
    //todo
//    @Override
//    public int getOpacity() {
//        return PixelFormat.TRANSLUCENT;
//    }

    @Override
    public void setAlpha(int alpha) {
        mPaint.setAlpha(alpha);
    }

    @Override
    public void setColorMatrix(ColorMatrix matrix) {
        super.setColorMatrix(matrix);
        mPaint.setColorMatrix(matrix);
    }

    public boolean ismUseStroke() {
        return mUseStroke;
    }

    public void setmUseStroke(boolean mUseStroke) {
        this.mUseStroke = mUseStroke;
    }

}